package com.nijie.library.service.impl.base;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nijie.library.DAO.base.BaseDao;
import com.nijie.library.POJO.Message;
import com.nijie.library.service.base.BaseSvc;

/**
 * ${DESCRIPTION}
 *
 * @author
 * @create 2017-12-03 21:08
 **/
@Service("baseSvc")
public class BaseSvcImpl<T> implements BaseSvc<T> {
	
	@Autowired
	private BaseDao<T>  baseDao;

	@Override
	public boolean getExist(T o) {
		return (baseDao.selectExist(o) > 0) ? true : false;
	}
	@Override
	public Message<T> save(T o) {
		Message<T> message = new Message<T>();
		if (getExist(o)) {
			message.setCode("201");
			message.setMsg("数据已存在！");
		} else {
			try {
				baseDao.insert(o);
				message.setCode("100");
				message.setMsg("保存成功！");
				message.setData(o);
			} catch (Exception e) {
				message.setCode("202");
				message.setMsg("保存发生意外错误！");
			}
		}
		return message;
	}

	@Override
	public Message<T> remove(T o) {
		return null;
	}

	@Override
	public Message<T> modify(T o) {
		Message<T> message = new Message<T>();
		if (getExist(o)) {
			message.setCode("201");
			message.setMsg("已存在，请重试！");
		} else {
			try {
				baseDao.update(o);
				message.setCode("101");
				message.setMsg("修改成功！");
				message.setData(o);
			} catch (Exception e) {
				message.setCode("202");
				message.setMsg("保存发生意外错误！");
			}
		}
		return message;
	}

	@Override
	public List<T> get(T o) {
		return baseDao.select(o);
	}
	@Override
	public Message<T> modifyStatus(String ids, String status) {
		// TODO Auto-generated method stub
		Message<T> message = new Message<T>();
		try {
			baseDao.updateStatus(ids, status);
			message.setCode("102");
			message.setMsg("删除成功！");
		} catch (Exception e) {
			message.setCode("202");
			message.setMsg("删除发生意外错误！");
		}
		return message;
	}
}
